<?php
/**
 * Description of TaxonomyModelclass
 *
 * @author daigangbo <diagangbo@126.com>
 */
class TaxonomyModel extends BaseModel {

    //自动验证
    protected $_validate         =         array(
            array('taxonomy_name','require','分类名称不能为空！'), // 在新增的时候验证name字段是否唯一
    );

    //自动完成
    protected $_auto = array (
            array ('create_time', 'time', 1, 'function' ) // 对created_on字段在新增的时候
    );
    /**
     * 分类列表
     * @param <type> $condation
     */
    public function getList($condation = array('pid'=>'','indent' => '&nbsp;-&nbsp;')) {

        return $this->getTreeList($condation);
    }
    protected function getTreeList($condation = array('pid'=>'')) {
        $sql = "SELECT node.taxonomy_id,CONCAT( REPEAT('{$condation['indent']}', (COUNT(parent.taxonomy_id) - 1) ), node.taxonomy_name) as name,count(parent.taxonomy_id)-1 as depth,node.lft,node.rgt
           FROM ";
        if($condation['pid']) {
            $sql .= table('taxonomy').' AS node,'.table('taxonomy').' AS parent,'.table('taxonomy').'as grandfather ';
            $sql .= 'where grandfather.taxonomy_id='.$condation['pid'].' and (parent.lft between grandfather.lft and grandfather.rgt) and (node.lft BETWEEN parent.lft AND parent.rgt)';
        }else {
            $sql .= table('taxonomy').' AS node,'.table('taxonomy').' AS parent';
            $sql .= ' where (node.lft BETWEEN parent.lft AND parent.rgt)';
        }
        $sql .= " GROUP BY node.taxonomy_id ORDER BY node.lft";

        $list = $this->query($sql);

        return $list;
    }

    /**
     * 添加子分类
     * @param $pid
     * @param $data['name'] = '';data['updated_at'] = 'Y-m-d H:i:s'
     */
    public function insertNode($pid, $data) {
        $this->query('LOCK TABLE '.table('taxonomy').' WRITE');
        //如果$pid为空则添加跟节点
        if(empty($pid)) {
            $rgt = $this->max('rgt');
            $data ['lft'] = $rgt + 1;
            $data ['rgt'] = $rgt + 2;
            $rs = $this->add ( $data);
            return $rs;
        }
        //增加分类
        $rs = $this->where ( 'taxonomy_id=' . $pid )->find ();
        if(!$rs) {
            $this->error = '找不到父节点';
            return false;
        }
        $lft = $rs ['lft'];
        $this->setField ( 'rgt', array ('exp', 'rgt+2' ), "rgt>$lft" );
        $this->setField ( 'lft', array ('exp', 'lft+2' ), "lft>$lft" );

        $data ['lft'] = $lft + 1;
        $data ['rgt'] = $lft + 2;
        //插入数据到taxonomy
        $rs = $this->add ( $data);
        if (false === $rs) {
            return false;
        }
        $this->query('UNLOCK TABLES');
        return $rs;
    }
    /**
     * 删除节点及其子节点
     */
    public function deleteNode($id) {
        $this->query('LOCK TABLE '.table('taxonomy').' WRITE');
        $rs = $this->find ( $id );
        $myLeft = $rs ['lft'];
        $myRight = $rs ['rgt'];
        $myWidth = $rs ['rgt'] - $rs ['lft'] + 1;

        $map ['lft'] = array ('exp', "BETWEEN $myLeft and $myRight" );
        if(!$this->where ( $map )->delete ()){
            return false;
        }

        $this->setField ( 'rgt', array ('exp', "rgt-$myWidth" ), "rgt>$myRight" );
        $this->setField ( 'lft', array ('exp', "lft-$myWidth" ), "lft>$myRight" );
        $this->query('UNLOCK TABLES');
        return true;
    }
    /**
     * 得到节点的的父节点,直至根
     */
    public function viewSinglePath($id) {
        $sql ="SELECT parent.name,parent.id
FROM ".tableName('taxonomy')." AS node,".
                tableName('taxonomy')." AS parent
WHERE node.lft BETWEEN parent.lft AND parent.rgt
AND (node.name = '$id' or node.id='$id')
ORDER BY parent.lft;";
        return $this->query($sql);

    }
}
?>
